<?php
// +----------------------------------------------------------------------
// | N+ Admin
// +----------------------------------------------------------------------
// | 作者: 神奇的汤圆 <1351218627@qq.com>
// +----------------------------------------------------------------------
// | 开源时间: 2022年1月1日
// +----------------------------------------------------------------------
// | 开源协议 ( https://mit-license.org )
// +----------------------------------------------------------------------
// | gitee 代码仓库：https://gitee.com/PHPchengxuyuan/yii_n
// +----------------------------------------------------------------------

// +----------------------------------------------------------------------
// | 全局助手函数
// +----------------------------------------------------------------------
/**
 * @param $over_time
 * @return array
 * 处理所有关联重复时间
 */
function BetweenDate($over_time)
{
    list($begin, $after) = explode(' - ', $over_time);
    $callback = function ($value, $type) {
        if ($type === 'after') {
            return "{$value} 23:59:59";
        } else {
            return "{$value} 00:00:00";
        }
    };
    if (is_callable($callback)) {
        $after = call_user_func($callback, $after, 'after');
        $begin = call_user_func($callback, $begin, 'begin');
    }
    return ['after' => $after, 'begin' => $begin];
}

function is_error($data)
{
    if (empty($data) || !is_array($data) || !array_key_exists('errno', $data) || (array_key_exists('errno', $data) && 0 == $data['errno'])) {
        return false;
    } else {
        return true;
    }
}

/**
 * 获取http
 * @return string
 */
function get_http()
{
    $http_type = ((isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] == 'on') || (isset($_SERVER['HTTP_X_FORWARDED_PROTO']) && $_SERVER['HTTP_X_FORWARDED_PROTO'] == 'https')) ? 'https://' : 'http://';
    return $http_type;
}

/**
* 删除旧文件  并加入到新文件夹中(含修改名字)
* @param $url_file
* @param $new_path
* @return mixed
    */
function rename_file($old_file, $new_path)
{
    if (file_exists($old_file)) {
        try {
            $bool = rename($old_file, $new_path);
            return $bool ? $new_path : $old_file;
        } catch (\Exception $th) {
            $bool = copy($old_file, $new_path);
            return $new_path;
        }
    }
    return $old_file;

}

/**
 * @param $image_file
 * @return string
 * 图片转base64
 */
function base64EncodeImage ($image_file) {
    $image_info = getimagesize($image_file);
    $image_data = fread(fopen($image_file, 'r'), filesize($image_file));
    $base64_image = 'data:' . $image_info['mime'] . ';base64,' . chunk_split(base64_encode($image_data));
    return $base64_image;
}


function add_val($int,$str){
    $arr = [];
    for ($i = 0; $i < $int; $i++){
        $arr[$i] = $str;
    }
    return $arr;
}

function calculate_img($val,$str){
    $val[$str] =  get_http().$_SERVER['SERVER_NAME'].'/'.$val[$str];
    return $val;
}



function gjj($str)
{
    $farr = array(
        "/\\s+/",
        "/<(\\/?)(script|i?frame|style|html|body|title|link|meta|object|\\?|\\%)([^>]*?)>/isU",
        "/(<[^>]*)on[a-zA-Z]+\s*=([^>]*>)/isU",
    );
    $str = preg_replace($farr,"",$str);
    return addslashes($str);
}

/**
 * @param $array
 * @return array|mixed|string
 * 防止注入
 */
function hg_input_bb($array)
{
    if (is_array($array))
    {
        foreach($array AS $k => $v)
        {
            $array[$k] = hg_input_bb($v);
        }
    }
    else
    {
        $array = gjj($array);
    }
    return $array;
}
//$_REQUEST = hg_input_bb($_REQUEST);
//$_GET = hg_input_bb($_GET);
//$_POST = hg_input_bb($_POST);


/**
 *  根据身份证号码获取性别
 *  author:xiaochuan
 *  @param string $idcard    身份证号码
 *  @return int $sex 性别 1男 2女 0未知
 */
function get_sex($idcard) {
    if(empty($idcard)) return null;
    $sexint = (int) substr($idcard, 16, 1);
    return $sexint % 2 === 0 ? '女' : '男';
}


/**
 *  根据身份证号码获取生日
 *  author:xiaochuan
 *  @param string $idcard    身份证号码
 *  @return $birthday
 */
function get_birthday($idcard) {
    if(empty($idcard)) return null;
    $bir = substr($idcard, 6, 8);
    $year = (int) substr($bir, 0, 4);
    $month = (int) substr($bir, 4, 2);
    $day = (int) substr($bir, 6, 2);
    return $year . "-" . $month . "-" . $day;
}

/**
 *  获取随机数
 */
if (!function_exists('make_password')) {
    function make_password($length = 8)
    {
        // 密码字符集，可任意添加你需要的字符
        $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
            'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
            't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
            'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
            'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
        );
        // 在 $chars 中随机取 $length 个数组元素键名
        $keys = array_rand($chars, $length);
        $password = '';
        for ($i = 0; $i < $length; $i++) {
            // 将 $length 个数组元素连接成字符串
            $password .= $chars[$keys[$i]];
        }
        return $password;
    }
}

/**
 * post_curl 提交
 */
if (!function_exists('curl_post')) {
    function curl_post($url, $data = '', $headers = [], $bool = false, $way = 'POST')
    {
        $curl = curl_init();
        $content = [
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => $way,
            CURLOPT_POSTFIELDS => $data,
            CURLOPT_HTTPHEADER => $headers
        ];
        if ($bool == true) {
            $content[CURLOPT_SSL_VERIFYPEER] = false;
            $content[CURLOPT_SSL_VERIFYHOST] = false;
        }
        curl_setopt_array($curl, $content);
        $response = curl_exec($curl);
        curl_close($curl);
        return $response;
    }
}

/**
 * 数据脱敏
 * @param $string 需要脱敏值
 * @param int $start 开始
 * @param int $length 结束
 * @param string $re 脱敏替代符号
 * @return bool|string
 * 例子:
 * dataDesensitization('13126989876', 3, 4); //131****9876
 * dataDesensitization('张三四', 0, -1); //**四
 */
function dataDesensitization($string, $start = 0, $length = 0, $re = '*')
{
    if (empty($string)){
        return false;
    }
    $strarr = array();
    $mb_strlen = mb_strlen($string);
    while ($mb_strlen) {//循环把字符串变为数组
        $strarr[] = mb_substr($string, 0, 1, 'utf8');
        $string = mb_substr($string, 1, $mb_strlen, 'utf8');
        $mb_strlen = mb_strlen($string);
    }
    $strlen = count($strarr);
    $begin = $start >= 0 ? $start : ($strlen - abs($start));
    $end = $last = $strlen - 1;
    if ($length > 0) {
        $end = $begin + $length - 1;
    } elseif ($length < 0) {
        $end -= abs($length);
    }
    for ($i = $begin; $i <= $end; $i++) {
        $strarr[$i] = $re;
    }
    if ($begin >= $end || $begin >= $last || $end > $last) return false;
    return implode('', $strarr);
}

/**
 * 验证是否存在文件夹
 */
function isDirFile($path, $isfile = false)
{
    if ($isfile) {
        return file_exists($path) ? true : false;
    }
    return is_dir($path) ? true : false;
}

/**
 * 获取本日、本周、本月
 */
function totimes()
{
    // 本日
    $time['day']['s'] = strtotime(date('Ymd'));
    $time['day']['e'] = $time['day']['s'] + 86400;
    // 本周
    $time['week']['s'] = strtotime("last Sunday +1 day");
    $time['week']['e'] = strtotime("next Sunday");
    // 本月
    $time['month']['s'] = strtotime(date("Ym01"));
    $time['month']['e'] = strtotime(date("Ymt") . " +1 day");
    return $time;
}